#ifndef	WF_VOLATILITY_GLOBALS
#define	WF_VOLATILITY_GLOBALS
#include <includes.h>
#include "WF_Volatility.h"

static uint8_t ShutDownAlarm_WorkStepHandle(uint8_t *TempWFStep,uint8_t *TempWFPeriod,uint8_t *WFStaChange,uint8_t Solution)
{
  if(GetFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_GenWorkingSta,GenWorkStaOrd_AlarmShutDown))//检查控制器的运行状态是否在停机告警态
  {
  	if(Solution&GenFaultWay_Action)
    {
        SetNewWorkFlowStepStatus(TempWFStep,TempWFPeriod,WFStaChange,VolatilityStep_endStep);
        return(DEF_TRUE);
    }
  }
  
  return(DEF_FALSE);
}

/**
 * @brief 
 * 
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempWFPeriod 
 * @param TempOutAlarm 
 * @return uint8_t 
 */
uint8_t VolatilityStep_1st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;
    CurrProcCountdownEnable = DEF_TRUE;
    SetNextNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,VolatilityStep_Maximum);
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}
/**
 * @brief 合闸前检查
 * 
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempWFPeriod 
 * @param TempOutAlarm 
 * @return uint8_t 
 */
uint8_t VolatilityStep_2st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;
    ActInform |= SwitchBefCheckStep(TempWorkflow,TempWFStep,TempWFPeriod,VolatilityStep_Maximum,
        share_parameterCfgData_st.volatilityCfg_st.EngineInitStart_time,share_parameterCfgData_st.volatilityCfg_st.EngineInitSpeed,ShutDownAlarm_WorkStepHandle);
    return (ActInform);
}
/**
 * @brief 波动数据采集
 * 
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempWFPeriod 
 * @param TempOutAlarm 
 * @return uint8_t 
 */
uint8_t VolatilityStep_3st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;

    /* AlarmShutDown     */
    if(ShutDownAlarm_WorkStepHandle(&TempWFStep,&TempWFPeriod,&WFStaChange,GenFaultWay_ALL))
    {
        UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
        return (ActInform);
    }

    switch (TempWFPeriod)
    {
    case WorkflowPeriod_Entry:
        // 等待稳定
        SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);
        SMTimerCounter = (uint32_t)share_parameterCfgData_st.volatilityCfg_st.WaitVoltStab_time * WorkFlowTimingFreq;
        break;
    case WorkflowPeriod_1st:
        if(SMTimerCounter==0)
        {
            SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_2nd);
        }
        break;
    case WorkflowPeriod_2nd:
        // 波形
        ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_Waveform,BitOperaOpt_Set);
        // 期间数据
        ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_VolatilityCollect,BitOperaOpt_Set);
        ActInform |= WorkflowOutRequestBit_WaveRequest;

        SMTimerCounter = (uint32_t)share_parameterCfgData_st.volatilityCfg_st.VolatTest_time * WorkFlowTimingFreq;
        SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_3nd);
        break;
    case WorkflowPeriod_3nd:
        if(SMTimerCounter==0)
        {
            SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_4nd);
        }
        break;
    case WorkflowPeriod_4nd:
        ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_WaveformEnd,BitOperaOpt_Set);
        ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_VolatilityCollectEnd,BitOperaOpt_Set);
        ActInform |= WorkflowOutRequestBit_WaveRequest;
        SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_5nd);
        break;
    case WorkflowPeriod_5nd:
        // 波动数据
        share_VolatilityData_st.baseData_st.SteadyVoltage_U=TemSUM_GenPhaseVoltL1/TemCount;
        share_VolatilityData_st.baseData_st.SteadyVoltage_V=TemSUM_GenPhaseVoltL2/TemCount;
        share_VolatilityData_st.baseData_st.SteadyVoltage_W=TemSUM_GenPhaseVoltL3/TemCount;
        share_VolatilityData_st.baseData_st.SteadyAverageVolt=TemSUM_GenPhaseVoltAverage/TemCount;
        // 计算
        share_VolatilityData_st.baseData_st.MaxVoltVolat_U=TemMax_GenPhaseVoltL1;
        share_VolatilityData_st.baseData_st.MaxVoltVolat_V=TemMax_GenPhaseVoltL2;
        share_VolatilityData_st.baseData_st.MaxVoltVolat_W=TemMax_GenPhaseVoltL3;
        share_VolatilityData_st.baseData_st.MaxVoltVolatility=TemMax_GenPhaseVoltAverage;
        share_VolatilityData_st.baseData_st.MinVoltVolat_U=TemMin_GenPhaseVoltL1;
        share_VolatilityData_st.baseData_st.MinVoltVolat_V=TemMin_GenPhaseVoltL2;
        share_VolatilityData_st.baseData_st.MinVoltVolat_W=TemMin_GenPhaseVoltL3;
        share_VolatilityData_st.baseData_st.MinVoltVolatility=TemMin_GenPhaseVoltAverage;
        // 频率波动最大值
        share_VolatilityData_st.baseData_st.MaxFreqVolat = TemMax_GenFreqAverage;
        // 频率波动最小值
        share_VolatilityData_st.baseData_st.MinFreqVolat = TemMin_GenFreqAverage;
        // 频率波动平均值
        share_VolatilityData_st.baseData_st.AverageFreq = TemSUM_GenFreqAverage/TemCount;

        // 相对额定值的线路 1 稳态电压偏差
        share_VolatilityData_st.baseData_st.VoltDiff_RelaRated_U = share_VolatilityData_st.baseData_st.SteadyVoltage_U - share_parameterCfgData_st.nominalCfg_st.RatedPhaseVolt;
        // 相对额定值的线路 2 稳态电压偏差
        share_VolatilityData_st.baseData_st.VoltDiff_RelaRated_V = share_VolatilityData_st.baseData_st.SteadyVoltage_V - share_parameterCfgData_st.nominalCfg_st.RatedPhaseVolt;
        // 相对额定值的线路 3 稳态电压偏差
        share_VolatilityData_st.baseData_st.VoltDiff_RelaRated_W = share_VolatilityData_st.baseData_st.SteadyVoltage_W - share_parameterCfgData_st.nominalCfg_st.RatedPhaseVolt;
        // 相对额定值的平均稳态电压偏差
        share_VolatilityData_st.baseData_st.VoltDiff_RelaRated = share_VolatilityData_st.baseData_st.SteadyAverageVolt - share_parameterCfgData_st.nominalCfg_st.RatedPhaseVolt;
        if(share_parameterCfgData_st.nominalCfg_st.RatedPhaseVolt)
        {
            // 相对额定值的线路 1 稳态电压偏差率
            share_VolatilityData_st.baseData_st.VoltDeviat_RelaRated_U = (share_VolatilityData_st.baseData_st.VoltDiff_RelaRated_U / share_parameterCfgData_st.nominalCfg_st.RatedPhaseVolt)*100;
            // 相对额定值的线路 2 稳态电压偏差率
            share_VolatilityData_st.baseData_st.VoltDeviat_RelaRated_V = (share_VolatilityData_st.baseData_st.VoltDiff_RelaRated_V / share_parameterCfgData_st.nominalCfg_st.RatedPhaseVolt)*100;
            // 相对额定值的线路 3 稳态电压偏差率
            share_VolatilityData_st.baseData_st.VoltDeviat_RelaRated_W = (share_VolatilityData_st.baseData_st.VoltDiff_RelaRated_W / share_parameterCfgData_st.nominalCfg_st.RatedPhaseVolt)*100;
            // 相对额定值的平均稳态电压偏差率
            share_VolatilityData_st.baseData_st.VoltDeviat_RelaRated = (share_VolatilityData_st.baseData_st.VoltDiff_RelaRated / share_parameterCfgData_st.nominalCfg_st.RatedPhaseVolt)*100;
            // 相对额定的频率差
            share_VolatilityData_st.baseData_st.FreqDiff_RelaRated = share_VolatilityData_st.baseData_st.AverageFreq - share_parameterCfgData_st.nominalCfg_st.RatedFrequency;
            // 相对额定的频率差百分比
            share_VolatilityData_st.baseData_st.FreqDeviat_RelaRated = (share_VolatilityData_st.baseData_st.FreqDiff_RelaRated / share_parameterCfgData_st.nominalCfg_st.RatedFrequency)*100;
        }
        SetNextNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,VolatilityStep_endStep);
        break;
    }


    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}

uint8_t VolatilityStep_end(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;
    uint8_t WorkflowMode = TempWorkflow;
    if (TempWFPeriod == WorkflowPeriod_Entry)
    {
        SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);
        CurrProcCountdownEnable=DEF_FALSE;
        ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_WaveformEnd,BitOperaOpt_Set);
        ActInform |= WorkflowOutRequestBit_WaveRequest;
    }
    else
    {
        FlowDataStatus |= (1<<WorkflowMode_Volatility);
        if(GetFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_GenWorkingSta,GenWorkStaOrd_AlarmShutDown))
            SetNewWorkflowModeStatus(&TempWorkflow,&TempWFStep,&TempWFPeriod,&WFStaChange,WorkflowMode_StopPriMover);
        else
        {
            //正常停机计算数据
            SetNewWorkflowModeStatus(&TempWorkflow,&TempWFStep,&TempWFPeriod,&WFStaChange,WorkflowMode_Ending);
            WaveSetFlowDataStatus(WorkflowMode);
        }
    }
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}

typedef uint8_t (*WorkFlowStepType)(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm);
static const WorkFlowStepType VolatilityStepTable[] = {
    (WorkFlowStepType)VolatilityStep_1st        ,
    (WorkFlowStepType)VolatilityStep_2st        ,
    (WorkFlowStepType)VolatilityStep_3st        ,
    (WorkFlowStepType)VolatilityStep_end        ,
};


uint8_t WorkFlowHandling_Volatility(uint8_t TempWorkflow, uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE, WFStaChange = DEF_FALSE;

	ActInform |= (*VolatilityStepTable[TempWFStep])(TempWorkflow,TempWFStep,TempWFPeriod, TempOutAlarm);

    /************************************/
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}
#endif